package com.cgsd.heyu.utils;

import com.android.library.utils.ArithUtil;

import java.util.Collection;
import java.util.Map;

/**
 * @author xiaoxing
 * Description 数据格式
 * CreateData: 2020/12/24
 */
public class FormatUtil {
    /**
     * 判断手机号码
     */
    public static boolean isMobileNo(String mobiles) {
        if (mobiles == null){
            return false;
        }
        return mobiles.length() == 11;
    }

    /**
     * 是否是空字符串
     */
    public static boolean isEmpty(String str){
        if (str == null || "".equals(str)){
            return true;
        }
        return false;
    }

    /**
     * 替换字符格
     * @param str 原字符串
     * @param reStr 替换字符串
     * @param start 开始位置
     * @param end 接收位置
     * @return
     */
    public static String replace(String str,String reStr,int start,int end){
        if (str == null || str.length() < end || str.length() < start || end < start){
            return str;
        }
        StringBuilder builder = new StringBuilder(str);
        return (builder.replace(start,end,reStr)).toString();
    }

    /**
     * 电话号码
     * @param phone 电话号码
     * @return
     */
    public static String getPhone(String phone){
        return replace(phone,"****",3,7);
    }

    /**
     * 判断数据是否为空
     * @param obj
     * @return
     */
    @SuppressWarnings("all")
    public static boolean isEmpty(Object obj) {
        boolean flag = true;
        if (obj != null) {
            if (obj instanceof String) {
                flag = (obj.toString().trim().length() == 0);
                String tmp = obj.toString();
                flag = "".equals(tmp.trim());

            } else if (obj instanceof Collection<?>) {
                flag = ((Collection) obj).size() == 0;

            } else if (obj instanceof Map) {
                flag = ((Map) obj).size() == 0;

            } else if (obj instanceof Object[]) {
                flag = ((Object[]) obj).length == 0;

            } else {
                flag = false;
            }
        }
        return flag;
    }
    /**
     * 判断数据是否不为空
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 分转元
     * @param price
     * @return
     */
    public static String getPrice(String price){
        if (price == null){
            return "0";
        }
        try {
            return ArithUtil.div(price,"100",2);
        }catch (Exception e){
            return "0";
        }
    }


}
